import { openNewWindowMap } from "./window";
const channelEnum = {};
Object.keys(openNewWindowMap).forEach((path) => {
  channelEnum[path] = {
    name: DiagonalToHorizontal(path, true),
    comment: path,
  };
});
export const Channel = {
  channelMap: new Map(),
  send: (channelName, object) => {
    if (!Channel.channelMap.has(channelName)) {
      let channel = new BroadcastChannel(channelName);
      Channel.channelMap.set(channelName, channel);
    }
    Channel.channelMap.get(channelName).postMessage(object);
  },
  listen: (channelName, callback) => {
    if (!Channel.channelMap.has(channelName)) {
      let channel = new BroadcastChannel(channelName);
      Channel.channelMap.set(channelName, channel);
    }
    Channel.channelMap.get(channelName).addEventListener("message", callback);
  },
  close: (channelName) => {
    if (Channel.channelMap.has(channelName)) {
      Channel.channelMap.get(channelName).close();
      Channel.channelMap.delete(channelName);
    }
  },
  removeEvent(channelName, callback) {
    if (Channel.channelMap.has(channelName)) {
      Channel.channelMap
        .get(channelName)
        .removeEventListener("message", callback);
    }
  },
  channelEnum,
};
